package Tega::DBWrapper;

# $Id: DBWrapper.pm 52 2010-11-13 10:12:20Z dolbanavt@googlemail.com $
# $Date: 2010-11-13 12:12:36 +0200 (Суб, 13 Ноя 2010) $
# $HeadURL: https://tegusigalpa.googlecode.com/svn/branches/storage_init/lib/Tega/DBWrapper.pm $
# $LastChangedRevision: 67 $

use parent qw( JIP::BaseClass Class::Accessor::Fast );
use JIP::ToolSet;
use English qw( -no_match_vars );
use JIP::Utils qw( check_named_params );
use Carp qw( croak );

our $VERSION = '0.0.1';

__PACKAGE__->follow_best_practice;

__PACKAGE__->mk_accessors( qw( connection ) );

sub _init {
    my ( $self, $params_ref ) = @ARG;

    check_named_params(
        rules_ref  => { connection => { required => 1, defined  => 1 } },
        params_ref => $params_ref,
    );

    $self->set_connection( $params_ref->{'connection'} );

    return $self;
}

sub add_seen_action {
    my $self  = shift;
    my %param = @ARG;

    check_named_params(
        rules_ref  => {
            name   => { required => 1, defined  => 1 },
            action => {
                required => 1,
                defined  => 1,
                allow    => [ qw( join rename already part quit kick ban ) ],
            },
        },
        params_ref => \%param,
    );

    my $dbh = $self->get_connection->get_db_handle;

    $dbh->do(
        q{INSERT INTO seen_stat ( name, action ) VALUES ( ?, ? )},
        {},
        $param{'name'},
        $param{'action'},
    ) or croak 'cannot insert data: '. $dbh->errstr;

    return 1;
}

1;

__END__

=head1 NAME

Tega::DBWrapper - обертка скрывающая работу с базой данных от
подсистем проекта.

=head1 SYNOPSIS

    use Tega::DBWrapper;

    my $w = Tega::DBWrapper->new(
        connection => SQLWrapper->new(),
    );

    $w->get_connection->get_db_handle->do();

=head1 DEPENDENCIES

Class::Accessor::Fast (from CPAN)

JIP::ToolSet (from base_lib)

JIP::BaseClass (from base_lib)

JIP::Utils (from base_lib)

=head1 AUTHOR

Vladimir Zhavoronkov, <dolbanavt (at) Google Mail>

http://dolbanavt.livejournal.com/

=head1 LICENSE AND COPYRIGHT

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut
